package p.ithorns.support.license.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import p.ithorns.support.license.model.AuthReq;
import p.ithorns.support.license.model.LicenseBody;
import p.ithorns.support.license.service.GrantService;

/**
 * LicenseController
 *
 * @author Ht.L
 * @date 2023-12-13 09:43
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class LicenseServerController {

    private final GrantService service;


    /**
     * 服务端生成授权文件
     *
     * @param req -请求参数
     * @return ResponseEntity
     */
    @PostMapping("/auth")
    public ResponseEntity<String> genAuthorize(@RequestBody AuthReq req) throws Exception {
        return service.genAuthorize(req);
    }

    /**
     * 客户端发起证书请求
     *
     * @param clientId - 客户ID
     * @return LicenseBody
     */
    @PostMapping("/lic")
    public LicenseBody genLicense(@RequestParam("client-id") String clientId,
                                  @RequestParam("auth-code") String authCode) {
//        return Mono.fromCallable(() -> service.getLicense(id, rsaSnCodeEncrypt))
//                .subscribeOn(Schedulers.boundedElastic())
//                .onErrorResume(Mono::error)
//                .cache();
        return service.generateLicense(clientId, authCode);
    }

}